/**
 * 
 */
package client;

import java.rmi.Naming;
import java.rmi.RemoteException;

import com.IBattleShipServer;

import data.Constants;
import data.Ship.Orientation;

/**
 * @author admin
 *
 */
public class BattleShipClient 
{
	private IBattleShipServer server;
	private int ID;
	private String ip="";
	private String lookupAdress;
	
	public BattleShipClient(String IP)
	{
		ip = IP;
		lookupAdress = "rmi://" + ip + "/" + Constants.SERVER_NAME;
	}
	
	public boolean FoundServer()
	{
		try
		{
			Naming.lookup(lookupAdress);
			return true;
		}
		catch (RemoteException re) 
		{
			re.printStackTrace();
			return false;
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return false;
		}
	}
	
	public void LogIn()
	{
		try
		{
			server = (IBattleShipServer) Naming.lookup(lookupAdress);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	
	private boolean AccountFree()
	{
		if(server != null)
		try
		{
			return server.AccountFree();
		} catch (RemoteException e)
		{
			e.printStackTrace();
		}
		return false;
	}
	
	public boolean Register()
	{
		if(server != null)
		if(AccountFree())
		{
			try
			{
				ID = server.Register();
			} catch (RemoteException e)
			{
				e.printStackTrace();
			}
			return true;
		}
		return false;
	}
	
	public boolean PlayerReady()
	{
		if(server != null)
		try
		{
			return server.PlayerReady();
		} catch (RemoteException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
	
	public int NextShipLength()
	{
		if(server != null)
		try
		{
			return server.NextShipLength(ID);
		} catch (RemoteException e)
		{
			e.printStackTrace();
		}
		return 0;
	}
	
	public boolean AllowedToSetAnotherShip()
	{
		if(server != null)
		try
		{
			return server.AllowedToSetAnotherShip(ID);
		} catch (RemoteException e)
		{
			e.printStackTrace();
		}
		return false;
	}
	
	public boolean SetShipAtKoord(int x, int y, int length, Orientation orientation)
	{
		int ori = ((orientation == Orientation.Horizontal) ? 0 : 1);
		if(server != null)
		try
		{
			return server.SetShipAtKoord(x, y, length, ori, ID);
		} catch (RemoteException e)
		{
			e.printStackTrace();
		}
		return false;
	}
	
	public boolean IsItMyMove()
	{
		if(server != null)
		try
		{
			return server.IsItMyMove(ID);
		} catch (RemoteException e)
		{
			e.printStackTrace();
		}
		return false;
	}
	
	public boolean AgressEnemy(int x, int y)
	{
		if(server != null)
		try
		{
			return server.AgressEnemy(x, y, ID);
		} catch (RemoteException e)
		{
			e.printStackTrace();
		}
		return false;
	}
	
	public boolean EnemyGameOver()
	{
		if(server != null)
		try
		{
			return server.EnemyGameOver(ID);
		} catch (RemoteException e)
		{
			e.printStackTrace();
		}
		return false;
	}
	
	public boolean GameOver()
	{
		if(server != null)
		try
		{
			return server.GameOver(ID);
		} catch (RemoteException e)
		{
			e.printStackTrace();
		}
		return false;
	}
	
	public int StateOfGameField(int x, int y)
	{
		if(server != null)
		try
		{
			return server.StateOfGameField(x, y, ID);
		} catch (RemoteException e)
		{
			e.printStackTrace();
		}
		return 0;
	}
	
	public int StateOfEnemyGameField(int x, int y)
	{
		if(server != null)
		try
		{
			return server.StateOfEnemyGameField(x, y, ID);
		} catch (RemoteException e)
		{
			e.printStackTrace();
		}
		return 0;
	}

}
